
//
//  UIImageView+SDWebImage.m
//  GeniusHelp_OC
//
//  Created by 8b410-2 on 16/11/7.
//  Copyright © 2016年 8b410-2. All rights reserved.
//

#import "UIImageView+SDWebImage.h"
#import <SDWebImage/SDImageCache.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIImage+Common.h"
@implementation UIImageView (SDWebImage)
- (void)abb_loadImageWithUrlStr:(NSString *)urlStr PlaceHolderImageStr:(NSString *)placeHolderImageStr Radius:(CGFloat)radius {
    /// 没有url就设置占位图并返回
    if (urlStr == nil) {
        self.image = [UIImage imageNamed:placeHolderImageStr];
        return;
    }
    /// url
    NSString *uStr = [NSString stringWithFormat:@"%@%@",kImageUrl,urlStr];
    NSURL *url = [NSURL URLWithString:uStr];
    /// 如果圆角不等于0
    if (radius != 0.0) {
        /// 先从缓存中查找有无该图片
        NSString *cacheurlStr = [NSString stringWithFormat:@"%@radiusCahe",uStr];
        [[SDImageCache sharedImageCache] diskImageExistsWithKey:cacheurlStr completion:^(BOOL isInCache) {
            if (isInCache) {
                UIImage *cacheImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:cacheurlStr];
                self.image = cacheImage;
            } else {
                [self sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:placeHolderImageStr] options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    if (!error) {
                        UIImage *radiusImage = [image abb_drawRoundedCornerRectWithRadius:radius SizeToFit:self.frame.size];
                        self.image = radiusImage;
                        [[SDImageCache sharedImageCache] storeImage:radiusImage forKey:cacheurlStr];
                        [[SDImageCache sharedImageCache] removeImageForKey:urlStr];
                    }
                }];
            }
        }];
        
    } else {
        [self sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:placeHolderImageStr]];
    }
}
@end
